package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.CategoryDao;
import com.example.dao.DishDao;
import com.example.dao.LoginDao;
import com.example.dao.SetmealDao;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.Employee;
import com.example.entity.Setmeal;
import com.example.example.CustomerException;
import com.example.service.CategoryService;
import com.example.service.LoginService;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private DishDao dishDao;
    @Resource
    private SetmealDao setmealDao;


    @Override
    public void add(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()).toString());
        categoryDao.insert(category);

    }

    @Override
    public void del(Long id) {
        //先根据id查询dish表和setmel表中是否存在关联数据
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        List<Dish> dish = dishDao.selectList(queryWrapper);
        if (!dish.isEmpty()){
            throw new CustomerException("该分类已关联其他菜品不能删除");
        }

        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        List<Setmeal> setmeals = setmealDao.selectList(queryWrapper1);
        if (!setmeals.isEmpty()){
            throw new CustomerException("该分类已关联其他套餐不能删除");
        }
    }

    @Override
    public List<Category> findAll() {
        return categoryDao.selectList(null);
    }


    /**
     *  http://localhost:8080/category/exportExcel
     * 作用： 导出所有的类别的Excel表格
     * 注意：如果需要做文件下载有两点要求
     *      1. 返回值类型必须是void
     *      2. 方法上必须使用@ResponseBody注解，或者你已经使用@RestController
     */
    @Override
    public void updateCategory(Category category) {

        categoryDao.updateById(category);
    }
}
